<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      /* 
      ①确定dp数组和下标含义
      dp[i][j]表示从(0,0)到(i,j)共有dp[i][j]条路径
      ②状态转移方程
      到(i,j)只能从(i-1,j)或者(i,j-1)开始，因此只需要两种路线相加
      dp[i][j]=dp[i-1][j]+dp[i][j-1]
      ③初始化dp数组
      i==0 dp[0][j]=1 j==0dp[i][0]=1
      ④遍历顺序，从左上往右下遍历

      */
      var uniquePaths = function (m, n) {
        let dp = new Array(m).fill().map(() => new Array(n).fill(0));
        for (let i = 0; i < m; i++) {
          dp[i][0] = 1;
        }
        for (let j = 0; j < n; j++) {
          dp[0][j] = 1;
        }
        for (let i = 1; i < m; i++) {
          for (let j = 1; j < n; j++) {
            dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
          }
        }
        return dp[m - 1][n - 1];
      };
    </script>
  </body>
</html>
